// pages/imgupload/imgupload.js
const app=getApp();
Page({
 
  data: {
    photoid:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      "photoid": options.photoid
    })
    //onsole.log(this.data.photoid);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
  ,
  // 使用拍照
  photograph:function(){
    var photoid = this.data.photoid;
    var _this = this;
    var sourceType ="camera";
    //==3图片作品多张上传
    if (photoid==3){ 
      _this.moveImgsUp(photoid, sourceType);  
    }else{
      wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths;
          wx.navigateTo({
            url: '../imgCut?photoid=' + photoid + "&imgfile=" + tempFilePaths
          })
        }
      }) 
    } 
  },
  // 使用相册选择
  selectphotos:function(){
    console.warn('selectphotos'); 
    var _this=this;
    var photoid = this.data.photoid;
    var sourceType='album';
    //==3图片作品多张上传
    if (photoid==3){ 
      _this.moveImgsUp(photoid, sourceType);
    }else{ 
      wx.chooseImage({
        count: 1,
        sizeType: ['compressed'],
        sourceType: ['album'],
        success: function (res) { 
          var tempFilePaths = res.tempFilePaths;
          console.log(tempFilePaths);
          var photoid = _this.data.photoid; 
            wx.navigateTo({
              url: '../imgCut?photoid=' + photoid + "&imgfile=" + tempFilePaths
            })  
        }
      })
    } 
  },
  moveImgsUp: function (photoid,sourceType){
    wx.chooseImage({
      count: 9,
      sizeType: ['compressed'],
      sourceType: [sourceType],
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        var pathArry = new Array;
        var isboole=true;
        wx.showLoading({
          title: '正在上传中...' 
        }) 
        var completeCount=0;
        for (var i = 0; i < tempFilePaths.length; i++) {  
          wx.uploadFile({
            url: app.globalData.UploadUrl,
            filePath: tempFilePaths[i],
            name: 'file',
            formData: {
              category: photoid
            },
            success: function (res) {
              var result = JSON.parse(res.data);
               console.log("result",result);
              if (result.code == 1) {
                pathArry.push(result.data);
              }
            },
            complete:function(){
              completeCount++;
              if (completeCount == tempFilePaths.length){
                var pathUrls = pathArry.join("|");
                app.http.POST("/MiniCardOpus/InsertOpus", { 'opusImgs': pathUrls }, (err, data) => {
                  if (data.code == 1) {
                    var pages= getCurrentPages();
                    console.log(pages[pages.length - 1]);
                    pages[pages.length-2].reload();
                    wx.navigateBack({
                      delta: 1
                    })
                    wx.hideLoading();
                  } else {
                    wx.showToast({
                      title: '上传失败,请再次上传',
                      icon: 'none',
                      duration: 2000
                    })
                  }
                })
              }
            }
          })
        }  
      } 
    }) 
  },


})